﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LayoutLoggingServiceTest
{
    class AutoSizeFormClass
    {
        public struct ControlRect
        {
            public int Left;
            public int Top;
            public int Width;
            public int Height;
        }
        public List<ControlRect> oldCtrl;

        public void ControllInitializeSize(Form mForm)
        {
            oldCtrl = new List<ControlRect>();
            ControlRect cR;
            cR.Left = mForm.Left;
            cR.Top = mForm.Top;
            cR.Width = mForm.Width;
            cR.Height = mForm.Height;
            oldCtrl.Add(cR);
            foreach (Control c in mForm.Controls)
            {
                ControlRect objCtrl;
                objCtrl.Left = c.Left;
                objCtrl.Top = c.Top;
                objCtrl.Width = c.Width;
                objCtrl.Height = c.Height;
                oldCtrl.Add(objCtrl);
                InitializeSize(c);
            }
        }

        public void ControlAutoSize(Form mForm)
        {
            float wScale = (float)mForm.Width / (float)oldCtrl[0].Width;
            float hScale = (float)mForm.Height / (float)oldCtrl[0].Height;
            int ctrLeft0, ctrTop0, ctrWidth0, ctrHeight0;
            int ctrlNo = 1;
            foreach (Control c in mForm.Controls)
            {
                ctrLeft0 = oldCtrl[ctrlNo].Left;
                ctrTop0 = oldCtrl[ctrlNo].Top;
                ctrWidth0 = oldCtrl[ctrlNo].Width;
                ctrHeight0 = oldCtrl[ctrlNo].Height;
                c.Left = (int)((ctrLeft0) * wScale);
                c.Top = (int)((ctrTop0) * hScale);
                c.Width = (int)(ctrWidth0 * wScale);
                c.Height = (int)(ctrHeight0 * hScale);
                ctrlNo += 1;
                ctrlNo = AutoSize(c, ctrlNo, wScale, hScale);
            }
        }

        private void InitializeSize(Control control)
        {
            foreach (Control c in control.Controls)
            {
                ControlRect sobjCtrl;
                sobjCtrl.Left = c.Left;
                sobjCtrl.Top = c.Top;
                sobjCtrl.Width = c.Width;
                sobjCtrl.Height = c.Height;
                oldCtrl.Add(sobjCtrl);
                if (c.Controls != null)
                {
                    InitializeSize(c);
                }
            }
        }

        private int AutoSize(Control control, int ctrlNo, float wScale, float hScale)
        {
            int ctrLeft0, ctrTop0, ctrWidth0, ctrHeight0;

            foreach (Control s in control.Controls)
            {
                ctrLeft0 = oldCtrl[ctrlNo].Left;
                ctrTop0 = oldCtrl[ctrlNo].Top;
                ctrWidth0 = oldCtrl[ctrlNo].Width;
                ctrHeight0 = oldCtrl[ctrlNo].Height;
                s.Left = (int)((ctrLeft0) * wScale);
                s.Top = (int)((ctrTop0) * hScale);
                s.Width = (int)(ctrWidth0 * wScale);
                s.Height = (int)(ctrHeight0 * hScale);
                ctrlNo += 1;
                if (s.Controls != null)
                {
                    ctrlNo = AutoSize(s, ctrlNo, wScale, hScale);
                }
            }
            return ctrlNo;
        }
    }
}
